package cn.itcast.xuesheng.web.Interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//拦截器不会拦截静态资源，只会拦截访问的控制器方法，就是userServlet的方法，然而过滤器/*拦截所有资源不管静态动态
public class MyInterceptor1 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //在执行方法前拦截
        //return true 放行 return false 不放行 return false就是拦截方法不执行，执行拦截器方法
        String uri = request.getRequestURI();
        if (uri.contains("/Login.html") || uri.contains("user/login") || uri.contains("/CheckCodeServlet")  || uri.contains("/register.html") || uri.contains("/user/")  || uri.contains("topic/")) {
            return true;
        }
        Object user = request.getSession().getAttribute("user");
        if (user!=null){
            return true;
        }
        response.sendRedirect("Login.html");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
     //方法执行后拦截器方法
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
     //拦截器最后执行方法
    }
}
